package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.mapper.SpcyWjWjtxjbxxMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 审评查验-问卷填写基本信息
 * @Author: yuncheng
 * @Date: 2023-05-05
 * @Version: V1.0
 */
@Service
public class SpcyWjWjtxjbxxServiceImpl extends BaseServiceImpl<SpcyWjWjtxjbxxMapper, SpcyWjWjtxjbxx> implements ISpcyWjWjtxjbxxService {

    @Resource
    @Lazy
    private ISpcyWjWjtxjbxxService spcyWjWjtxjbxxService;

    @Resource
    @Lazy
    private ISpcyWjWjtxtmService spcyWjWjtxtmService;

    @Resource
    @Lazy
    private ISpcyWjWjtxtmxxService spcyWjWjtxtmxxService;

    @Resource
    @Lazy
    private ISpcyWjWjtmService spcyWjWjtmService;

    @Resource
    @Lazy
    private ISpcyWjWjtmxxService spcyWjWjtmxxService;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Override
    public void initWjtxjbxx(String djId,String ysjId,SpcyWjWjjbxx ySjjbxx,SpcyWjWjtxjbxx wjtxjbxx) {
        SpcyWjWjtxjbxx spcyPxDjjbxx = new SpcyWjWjtxjbxx();
        spcyPxDjjbxx.setId(djId);
        spcyPxDjjbxx.setFtxrq(new Date());
        spcyPxDjjbxx.setFwjbt(ySjjbxx.getFwjbt());
        spcyPxDjjbxx.setFwjlx(ySjjbxx.getFwjlx());
        spcyPxDjjbxx.setFwjms(ySjjbxx.getFwjms());
        spcyPxDjjbxx.setFwjjbxxid(ysjId);
        spcyPxDjjbxx.setFbz(ySjjbxx.getFbz());

        spcyPxDjjbxx.setSysOrgCode(ySjjbxx.getSysOrgCode()); // 2023-6-26 新增

        // 填写人基本信息
        spcyPxDjjbxx.setFsfdwmc(wjtxjbxx.getFsfdwmc());
        spcyPxDjjbxx.setFsfdwdz(wjtxjbxx.getFsfdwdz());
        spcyPxDjjbxx.setFlxr(wjtxjbxx.getFlxr());
        spcyPxDjjbxx.setFlxdh(wjtxjbxx.getFlxdh());
        spcyWjWjtxjbxxService.save(spcyPxDjjbxx); // 新增填写的问卷
        spcyPxDjjbxx.setSysOrgCode(ySjjbxx.getSysOrgCode());
        spcyWjWjtxjbxxService.updateById(spcyPxDjjbxx);

        String operContent = "新增填写的问卷信息";
        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcyPxDjjbxx);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_WJ_WJTXJBXX,operContent,spcyPxDjjbxx.getId(), LogConstant.LOGS_WJ_WJTXJBXX, SpcyConstant.LOGS_SYS_TYPE_XZ);

        // 获取原来的问卷题目
        List<SpcyWjWjtm> ySjtmList = spcyWjWjtmService.list(new LambdaQueryWrapper<SpcyWjWjtm>()
                .eq(SpcyWjWjtm::getFwjjbxxid, ysjId)
                .orderByAsc(SpcyWjWjtm::getFxh));

        SpcyWjWjtxtm djtm = null;
        // 原试卷题目不为空
        for (SpcyWjWjtm sjtm : ySjtmList){
            djtm = new SpcyWjWjtxtm(); // 问卷题目
            // 复制原来问卷题目
            String djtmId = GetUuIdUtils.ReplaceUuId(); // 问卷题目id
            BeanUtil.copyProperties(sjtm,djtm);
            djtm.setId(djtmId);
            djtm.setFwjtxjbxxid(djId);
            spcyWjWjtxtmService.save(djtm); // 保存答卷题目

            if (StringUtils.isNotBlank(sjtm.getFsftxxdyj()) && sjtm.getFsftxxdyj().equals("是")){
                SpcyWjWjtxtmxx newTmxx = new SpcyWjWjtxtmxx();
                newTmxx.setId(GetUuIdUtils.ReplaceUuId());
                newTmxx.setFwjtxtmid(djtmId);
                newTmxx.setFxh(1);
                spcyWjWjtxtmxxService.save(newTmxx);
            }

            String ysjtmId = sjtm.getId(); // 原试卷题目id
            // 复制原试卷题目选项
            List<SpcyWjWjtmxx> ySjtmxxList = spcyWjWjtmxxService.list(new LambdaQueryWrapper<SpcyWjWjtmxx>()
                    .eq(SpcyWjWjtmxx::getFwjtmid, ysjtmId)
                    .orderByAsc(SpcyWjWjtmxx::getFxh));

            // 存在原试卷题目选项、新增答卷题目选项
            SpcyWjWjtxtmxx spcyPxDjda = null;
            for (SpcyWjWjtmxx sjtmxx : ySjtmxxList){
                spcyPxDjda = new SpcyWjWjtxtmxx();
                String djdaId = GetUuIdUtils.ReplaceUuId(); // 答卷答案id
                BeanUtil.copyProperties(sjtmxx,spcyPxDjda);
                spcyPxDjda.setId(djdaId);
                spcyPxDjda.setFwjtxtmid(djtmId); // 问卷题目id
                spcyPxDjda.setFxh(sjtmxx.getFxh());
                spcyWjWjtxtmxxService.save(spcyPxDjda); // 保存答卷题目选项
            }

        }
    }
}
